package com.jch.demo.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class LogAspect {

    public void before(JoinPoint joinPoint){
        System.out.println("before method: " + joinPoint.getSignature().getName() + " args: " + Arrays.asList(joinPoint.getArgs()));
    }

    public void after(JoinPoint joinPoint){
        System.out.println("after method: " + joinPoint.getSignature().getName()+ " end." );
    }

    public void afterReturning(JoinPoint joinPoint,Object result){
        System.out.println("afterReturning method: " + joinPoint.getSignature().getName() + " result: " + result);
    }

    public void afterThrowing(JoinPoint joinPoint,Exception e){
        System.out.println("afterThrowing method: " + joinPoint.getSignature().getName() + " exception: " + e);
    }

    public Object around(ProceedingJoinPoint pjd){
        System.out.println("begin around");
        Object result = null;
        String methodName = pjd.getSignature().getName();
        try {
            // 前置通知
            System.out.println("method: " + methodName + " args: " + Arrays.asList(pjd.getArgs()) + " begin.");
            // 执行目标方法
            result = pjd.proceed();
            // 返回通知
            System.out.println("method: " + methodName + " result: " + result);
        } catch (Throwable throwable) {
            // 异常通知
            System.out.println("method: " + methodName + " throwable: " + throwable);
        }
        // 后置通知
        System.out.println("method: " + methodName + " end.");
        System.out.println("end around.");
        return result;
    }

}
